/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.app;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import wads.com.DocumentTemplateInfo;
import wads.com.WADTools;

public class DeActivateDTDialog extends JDialog{
//  private static final long serialVersionUID = 3099464485096098323L;
  
  private JList dtDisplayList = new JList();
  private JPanel detailPanel = new JPanel();
  private JLabel dtnumberLabel = new JLabel();
  private JLabel dtcreatedLabel = new JLabel();
  private JLabel detailLabel = new JLabel();
  private JButton removeBtn = new JButton();
  private JButton closeBtn = new JButton();
  private Font labelFont = new Font("CordiaUPC",Font.PLAIN,17);

  private Vector dtInfoList;
  private Vector<String> dtNameList;

  public void initDisplay(){
    Enumeration en = dtInfoList.elements();
    dtNameList.clear();
    while(en.hasMoreElements()){
      DocumentTemplateInfo dtInfo = (DocumentTemplateInfo)en.nextElement();
      dtNameList.add(dtInfo.getTemplate_Name());
      if(dtInfoList.indexOf(dtInfo) == 0){
        //set information label for the first document template in list
        dtnumberLabel.setText(dtInfo.getTemplate_ID());
        dtcreatedLabel.setText(dtInfo.getCreated_Date().toString());
      }
    }
    dtDisplayList.setListData(dtNameList);
  }

  public DeActivateDTDialog(Frame parent, Vector dtInfoList) {
    super(parent);
    this.dtInfoList = dtInfoList;
    dtNameList = new Vector<String>();
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    initDisplay();
  }
  
  private void jbInit() throws Exception {
    this.setTitle("Deactivate Document Template");
    this.getContentPane().setLayout(null);
    dtDisplayList.setBounds(new Rectangle(12, 15, 210, 290));
    dtDisplayList.setFont(labelFont);
    dtDisplayList.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        dtDisplayList_mouseClicked(e);
      }
    });
    detailPanel.setBorder(BorderFactory.createEtchedBorder());
    detailPanel.setBounds(new Rectangle(250, 15, 230, 290));
    detailPanel.setLayout(null);
    detailPanel.setFont(labelFont);
    dtnumberLabel.setText("Document Template Number");
    dtnumberLabel.setBounds(new Rectangle(10, 45, 210, 20));
    dtnumberLabel.setFont(labelFont);
    dtnumberLabel.setForeground(Color.black);
    dtnumberLabel.setBorder(BorderFactory.createEtchedBorder());
    dtcreatedLabel.setText("Document Template Created Date");
    dtcreatedLabel.setBounds(new Rectangle(10, 80, 210, 20));
    dtcreatedLabel.setFont(labelFont);
    dtcreatedLabel.setForeground(Color.black);
    dtcreatedLabel.setBorder(BorderFactory.createEtchedBorder());
    detailLabel.setText("Detail");
    detailLabel.setBounds(new Rectangle(10, 6, 64, 17));
    detailLabel.setFont(labelFont);
    detailLabel.setForeground(Color.black);
    removeBtn.setText("Remove Template");
    removeBtn.setToolTipText("Remove Document Template from System");
    removeBtn.setBounds(new Rectangle(10, 240, 129, 27));
    removeBtn.setFont(labelFont);
    removeBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        removeBtn_actionPerformed(e);
      }
    });
    closeBtn.setText("Close");
    closeBtn.setBounds(new Rectangle(400, 330, 79, 27));
    closeBtn.setFont(labelFont);
    closeBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        closeBtn_actionPerformed(e);
      }
    });
    this.getContentPane().add(dtDisplayList, null);
    this.getContentPane().add(detailPanel, null);
    detailPanel.add(removeBtn, null);
    detailPanel.add(dtcreatedLabel, null);
    detailPanel.add(dtnumberLabel, null);
    detailPanel.add(detailLabel, null);
    detailPanel.setFont(labelFont);
    this.getContentPane().add(closeBtn, null);
    this.setSize(500, 400);
  }

  private void dtDisplayList_mouseClicked(MouseEvent e) {
    int index = dtDisplayList.getAnchorSelectionIndex();
    if(index >= 0){
      dtnumberLabel.setText(((DocumentTemplateInfo)dtInfoList.elementAt(index)).getTemplate_ID());
      dtcreatedLabel.setText(((DocumentTemplateInfo)dtInfoList.elementAt(index)).getCreated_Date().toString());
    }
  }

  private void removeBtn_actionPerformed(ActionEvent e) {
    int index = dtDisplayList.getAnchorSelectionIndex();
    int confirmationOption = JOptionPane.showConfirmDialog(this, "Are you sure want to REMOVE this template.",
        "Warning!!!", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
    if(index >= 0 && confirmationOption == JOptionPane.YES_OPTION){
      try {
        DocumentTemplateInfo dtInfo = (DocumentTemplateInfo) dtInfoList.elementAt(index);
        WADTools.deleteDIR(new File(WADTools.ACTIVATE_DIR_PATH + dtInfo.getTemplate_ID()));
        dtNameList.removeElementAt(index);
        dtInfoList.removeElementAt(index);
        dtDisplayList.updateUI();
      }catch(Exception ex){
        JOptionPane.showMessageDialog(null,
            "Can't deactivate the document template",
            "Alert", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
    }
    else
      return;
  }

  private void closeBtn_actionPerformed(ActionEvent e) {
    this.dispose();
  }
}